#include "widget.h"
#include "ui_widget.h"
#include <iostream>
#include <QWidget>
#include <QKeyEvent>
#include <QPaintEvent>
#include <QPainter>
#include <QPushButton>
#include <QTimer>
#include "snake.h"
#include <QMessageBox>
#include <QTime>
#include <QListWidget>


Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
    //加载图标

    setWindowIcon(QIcon(":/qtthings/rr"));

    //使用定时器
    timer=new QTimer();
    //定时器有timeout超时信号
    //信号槽(信号和一个对应的函数关联起来）
    connect(timer,SIGNAL(timeout()),this,SLOT(timeout()));
    this->resize(1800,1000);
    this->setFixedSize(1800,1000);

    //初始化奖品
    addNewReward();
    //初始化障碍物

    setWindowTitle("贪吃蛇游戏界面");
}

Widget::~Widget()
{
    delete ui;
}


//控制方向
void Widget::keyPressEvent(QKeyEvent *event)
{
    switch(event->key())
    {
    case Qt::Key_Up:
       if(MoveFlag!=DIR_DOWN){
       MoveFlag=DIR_UP;
       }
       break;
    case Qt::Key_Down:
        if(MoveFlag!=DIR_UP){
        MoveFlag=DIR_DOWN;
        }
       break;
    case Qt::Key_Right:
        if(MoveFlag!=DIR_LEFT){
        MoveFlag=DIR_RIGHT;
        }
       break;
    case Qt::Key_Left:
        if(MoveFlag!=DIR_RIGHT){
        MoveFlag=DIR_LEFT;
        }
       break;
    case Qt::Key_Space:
        if(gameStart==false)
        {
            gameStart=true;
            //start timer
            timer->start(time);

        }
        else{
            gameStart=false;
            timer->stop();
        }
       break;
     default:
        break;
    }
}
//超时信号
void Widget::timeout()
{

    int count=1;
    if(m.snake[0].intersects(rewardNode))
    {
        count++;
        addNewReward();
    }

    while(count--)
    {
    switch(MoveFlag){
      case DIR_UP:
          m.addTop();
        break;
      case DIR_DOWN:
          m.addDown();
        break;
      case DIR_LEFT:
          m.addLeft();
        break;
      case DIR_RIGHT:
          m.addRight();
        break;
     default:
        break;
    }
 }
    m.deleteLast();
    canshu=checkContact();
    canhsu2=m.snake.length();
    update();
}
void Widget::paintEvent(QPaintEvent *event){
    QPainter  painter(this);
    QPen pen;
    QBrush brush;
    QFont font("方正舒体",12,QFont::ExtraLight,false);
    //背景
    QPixmap pix;
    pix.load(":/qtthings/beijing");
    painter.drawPixmap(0,0,1800,1000,pix);


    //画蛇

    pen.setColor(Qt::black);
    brush.setColor(Qt::red);
    brush.setStyle(Qt::SolidPattern);
    painter.setPen(pen);
    painter.setBrush(brush);
    painter.drawEllipse(m.snake[0]);
    for(int i=1;i<m.snake.length();i++)
    {
        painter.drawRoundRect(m.snake[i]);
    }
    painter.drawLine(QPoint(0,0),QPoint(0,1000));
    painter.drawLine(QPoint(0,0),QPoint(0,1800));
    painter.drawLine(QPoint(1800,0),QPoint(1800,1000));
    painter.drawLine(QPoint(0,1000),QPoint(1800,1000));
    //画障碍物
    if(canhsu2>5&&canhsu2<10)
    {

        p.barriarnum=6;
    }
    if(canhsu2>=10&&canhsu2<=13)
    {

        p.barriarnum=8;
    }
    if(canhsu2>=13&&canhsu2<20)
    {

        p.barriarnum=10;
    }
    if(canhsu2>=20)
    {

        p.barriarnum=12;
    }

    pen.setColor(Qt::black);
    brush.setColor(Qt::green);
    brush.setStyle(Qt::SolidPattern);
    painter.setPen(pen);
    painter.setBrush(brush);

    for(int i=0;i<p.barriarnum;i++)
    {
        painter.drawRect(p.m_barrir[i]);
    }

    //draw画奖品
    pen.setColor(Qt::red);
    brush.setColor(Qt::darkMagenta);
    brush.setStyle(Qt::SolidPattern);
    painter.setPen(pen);
    painter.setBrush(brush);
    painter.drawEllipse(rewardNode);


   //绘制失败后的退出窗口
    if(canshu==1)
    {
        QFont font("方正舒体",30,QFont::ExtraLight,false);
        painter.setFont(font);
        painter.drawText((this->width()-500)/2,(this->height()-300)/2,QString("GAME OVER"));
        timer->stop();
        int isContinue = QMessageBox::question(this,"你的分数为"+QString("%1").arg(m.snake.length()-3),"是否游玩另一个模式",QMessageBox::Yes|QMessageBox::No,QMessageBox::Yes);
        if(isContinue==QMessageBox::Yes)
        {

            canshu=0;
            this->close();

        }
        else
        {
            exit(0);
        }

    }
    QWidget::paintEvent(event);
    pen.setColor(Qt::white);
        painter.setPen(pen);
        painter.setFont(font);
        painter.drawText(20,20,QString("当前得分：")+QString("%1").arg(m.snake.length()-3));

}


//判断是否游戏失败

int Widget::checkContact()
{
    int keck=0;
    for(int i=1;i<m.snake.length();i++)
    for(int j=i+1;j<m.snake.length();j++)
    {

        if(m.snake[i]==m.snake[j])
        {
            keck=1;
            break;
        }
    }
    if(m.snake[0].x()+nodeWidth>this->width()||m.snake[0].x()-nodeWidth<0||m.snake[0].y()-nodeHeight<0||m.snake[0].y()+nodeHeight>this->height())
    {
        keck=1;
    }
    for(int i=0;i<m.snake.length();i++)
        for(int j=0;j<p.barriarnum;j++)
    {
        if(m.snake[i].intersects(p.m_barrir[j]))
        {
            keck=1;
        }
    }
    return keck;

}
void Widget::addNewReward()
{
    QTime m_time=QTime::currentTime();
    qsrand(m_time.msec()+m_time.second()*1000);
    rewardNode=QRectF(qrand()%(this->width()/20)*20,qrand()%(this->height()/20)*20,nodeWidth,nodeHeight);
    for(int i=0;i<p.barriarnum;i++)
    {
    while(rewardNode.y()<40||rewardNode.x()<40||rewardNode.intersects(p.m_barrir[i]))
    {
        rewardNode=QRectF(qrand()%(this->width()/20)*20,qrand()%(this->height()/20)*20,nodeWidth,nodeHeight);
    }
    }

}

